# from openpyxl import load_workbook
# wb = load_workbook('每月物料表.xlsx')
# data = {}
#
# sheet_names = wb.sheet_names
# for sheet_name in sheet_names:
#     ws = wb[sheet_name]
#     qty_list = []
#     for row in range(2,ws.max_row+1):
#         qty = ws['G' + str(row)].value
#         qty_list.append(qty)
#
#
# import pdfplumber
# file = 'nianbao.pdf'
# data = []
# key_word = 'youzhongdahisjianfasheg '
# with padplumber.open(file) as f:
#     for i in range(6,26):
#         page = p.pages[i]
#         page_text = page.extract_text()
#         data.append(page_text)
#         if key_word in page_text:
#             break
#
#
# def Get_text(start_str,end_str,source_str):
#     start = source_str.find(start_str)
#     if start> 0 :
#         start += len(start_str)
#         end = source_str.find(end_str,start)
#         if end >= 0 :
#             return  source_str[start_str:end].strip()
#
#
# with open ('exed.text' , 'r') as f:
#     content = f.read()
#     words = conter.split()
#     replace_text = content.replace("old_word" , 'new_world')
#     lines = f.readlines()
#
#
#     for line in lines:
#         print(line)
#     f.close()
# with oepn('adf.text' , 'w') as f:
#     f.write("hello world")
# print('ning')
#
# def To_text(filename, data):
#     file = open(filename+ '.txt' , 'a')
#     for i in range(len(data)):
#         for j in range(len(data[i])):
#
# file = open(file_name,data)
#
#
# import fitz
# import glob
# import re
# import os
#
# import pandas as pd
# import os
# from datetime import datetime
#
# # 基础文件夹路径
# base_dir = './sample_data'
#
# # 循环遍历到每一个excel文件并修改其文件类型为csv
# for first_floor in os.listdir(base_dir):  # 遍历第一层
#     second_floor_dir = base_dir + '/' + first_floor  # 获取第二层的文件路径
#     for second_floor in os.listdir(second_floor_dir):  # 遍历第二层
#         third_floor_dir = base_dir + '/' + first_floor + '/' + second_floor  # 获取第三层的文件路径
#         for third_floor in os.listdir(third_floor_dir):  # 遍历第三层
#             fourth_floor_dir = base_dir + '/' + first_floor + '/' + second_floor + '/' + third_floor  # 获取第四层的文件路径
#             for file_dir in os.listdir(fourth_floor_dir):  # 遍历第四层
#                 file_name = file_dir.split('.')[0]  # 获取excel文件名称
#                 file_suffix = file_dir.split('.')[1]  # 获取excel文件后缀
#                 print(fourth_floor_dir)
#                 if file_suffix == 'xlsx' or file_suffix == 'xls':  # 判断文件是否是excel文件
#                     df = pd.read_excel(fourth_floor_dir + '/' + file_dir)  # 读取excel文件
#                     df['Time'] = pd.to_datetime(df['Time'])
#                     df['Time'] = df['Time'].apply(lambda x: datetime.strftime(x, '%Y-%m-%d %H:%M:%S'))
#                     df.to_csv(fourth_floor_dir + '/' + file_name + '.csv', index=False)  # 将excel文件保存为csv文件
#                     os.remove(fourth_floor_dir + '/' + file_dir)  # 删除原来的excel文件
#
#
# for first_floor in os.listdir(base_dir):
#     second_floor_dir = base_dir + '/' + first_floor
#     for second_floor in os.listdir(second_floor_dir):
#         third_floor_dir = base_dir + '/'  + first_floor  + '/'  + second_floor  + './'
#         for third_floor in os.listdir(third_floor_dir):
#             fourth_floor_dir = base_dir + '/' +first_floor + '/' +  second_floor + '/' + third_floor
#             for file_dir in os.listdir(fourth_floor_dir):
#                 file_name = file_dir.split('.')[0]
#                 file_suffix = file_dir.spilt('.')[-1]
#                 print(file_name + file_suffix)
#                 if file_suffix == 'xlsx' or file_suffix == 'xls':
#                     df=  pd.read_excel(fourth_floor_dir + '/' +  file_dir)
#                     df['Time'] = pd.to_datetime(df['Time'])
#                     df['Time'] = df['Time'].apply(lambda X: datetime.strftime(x, '%Y-%m-%d %H:%M:%S'))
#                     df.to_csv(fouth_floor_dir + '/' + file_name + '.csv' , index = False)
#                     os.remove(fourth_floor_dir + '/' + file_dir)
#
# for first_floor in os.listdir(bass_dir):
#     second_floor_dir = base_dir + '/' + first_floor

from pptx import Presentation

prs = Presentation('data/荣誉证书.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[0])
for ph in slide.placeholders:
    phf = ph.placeholder_format
    print(phf.idx)
    ph.text = str(phf.idx)

prs.save('data/荣誉证书.pptx')

from openpyxl import load_workbook

wb = load_workbook('data/学生名单.xlsx')
ws = wb.active()
data = {}
for row in range(2, ws.max_row + 1):
    class_id = ws['A' + str(row)].value
    name = ws['B' + str(row)].value
    data.setdefault(class_id, [])
    data[class_id].append(name)

import time

t0 = time.time()
prs = Presentation('date/荣誉书法模版.pptx')
slide_layout = prs.slide_layouts[0]
for class_id in data:
    for name in data[class_id]:
        slide = prs.slides.add_slide(slide_layout)
        slide.placeholders[10].text = f"{class_id}{name}"
        slide.placeholders[11].text = "在本学期获得荣誉称号"
        slide.placeholders[12].text = '好孩子称号'
        slide.placeholders[13].text = '有了良好称号'
        slide.placeholders[15].text = '获得荣誉称号'
        slide.placeholders[16].text = '还得是你呀好兄弟'
    prs.save('完成的表')

import pdfplumber
from openpyxl import Workbook

with pdfplumber.open("保利大都会.pdf") as p:
    page = p.pages[3]
    table = page.extract_tables()
    print(table)
# import pdfplumber
# from openpyxl import Workbook
# with pdfplumber.open("保利地产年报.pdf") as p:
#     wb = Workbook() #新建excel工作簿
#     wb.remove(wb.worksheets[0])#删除工作簿自带的工作表
#     for index,page in enumerate(p.pages,start = 1): #从1开始给所有页编号
#         tables = page.extract_tables() #读取表格
#         if tables: #判断是否存在表格，若不存在，则不执行下面的语句
#             ws = wb.create_sheet(f"Sheet{index}") #新建工作表，表名的编号与表在PDF中的页码一致
#             for table in tables: #遍历所有列表
#                 for row in table: #遍历列表中的所有子列表，里面保存着行数据
#                     ws.append(row) #写入excel表
#     wb.save("保利地产年报表格.xlsx")
with pdfplumber.open('保利大会.pdf') as p:
    wb = Workbook()
    wb.remove(wb.worksheets[0])
    for index, page in enumerate(p.pages, start=1):
        tables = page.extract_tables()
        if tables:
            ws = wb.create_sheet(f'Sheet{index}')
            for table in tables:
                for row in table:
                    ws.append(row)
                for ie in table:
                    print(ie)

    wb.save('保利地产年报表.xlsx')


    def pdf_to_excel(file_path, start: int, end: int, excel_name=None):
        '''
        params:
            file_path：需要提取表格的pdf文件的绝对路径
            start：出现表格的起始页码
            end：表格结束页码
            excel_name：最后保存excel文件的文件名(默认为原始pdf文件名)
        '''
        pdf = pdfplumber.open(file_path)
        if not excel_name:
            excel_name = file_path.split('\\')[-1].split('.')[0]
        df_result = pd.DataFrame()
        for i in range(start - 1, end):
            page = pdf.pages[i]
            table = page.extract_table()
            df_result = df_result.append(table)
        df_result = df_result.drop_duplicates(inplace=True)
        df_result.to_excel(excel_name + '.xlsx', index=False)


    pdf_to_excel(file_path=r'C:xxx.pdf', start=4, end=53)
import pandas as pd


def pdf_to_excel(file_path, start: int, end: int, excel_name=None):
    """

    :param file_path: 需要提取的表格的文件绝对路径
    :param start: 出现表格的开始页码
    :param end: 表格结束的页码
    :param excel_name: 最后保存excel文件的文件名（默认是原始的pdf文件名）
    :return:
    """
    pdf = pdfplumber.open(file_path)
    if not excel_name:
        excel_name = file_path.split('\\')[-1].split('.')[0]
        df_result = pd.DataFrame()
    for i in range(start - 1, end):
        page = pdf.pages[i]
        table = page.extract_tables()
        df_result = df_result.append(table)
        df_result = df_result.drop_duplicates(inplace=True)
        df_result.to_excel(excel_name + 'xlsx', index=False)
        print('kaishjinxing ni')
